package com.je180503.dao.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {

    private  static SqlSession sqlSession;

    private MyBatisUtil(){
    }

    public static SqlSession getSqlSession(){


        if (sqlSession==null){


            try {
                InputStream is  = Resources.getResourceAsStream("mybatis-config.xml");

                sqlSession=new SqlSessionFactoryBuilder().build(is).openSession(false);
            } catch (IOException e) {
                e.printStackTrace();

            }

        }

        return sqlSession;
    }


    public static void closeSqlSession(){
        if (sqlSession!=null){
            sqlSession.close();
        }sqlSession=null;

    }

}
